<template>
	<view class="addr">
		<view class="top-bg"></view>
		<view class="pl-3 pr-3 addr-wrap">
			<u-list @scrolltolower="scrolltolower" height="1168rpx">
				<u-list-item class="mb-3 pt-2  fb-item" v-for="(item,index) in addrList" :key="index">
					<view class="mb-1 pr-2 pl-2 fb-content">{{item.ProvName+item.CityName+item.TownName +item.Zone5}}
					</view>
					<view class="flex justify-between pr-2 pl-2 mb-2 header">
						<view>{{item.ContactName}}</view>
						<view>{{item.Mobile}}</view>
					</view>
					<view class="flex justify-between align-center p-2 opt">
						<view>
							<view v-if="item.IsDefault" class="default">默认地址</view>
						</view>
						<view class="flex justify-end">
							<u-button v-if="!item.IsDefault" @click="handleDefaultAddr(item)" type="primary" size="mini" text="设为默认"></u-button>
							<u-button @click="handleEditaddr(item)" size="mini" text="修改"></u-button>
							<u-button @click="handleModalConfirm(item)" size="mini" text="删除"></u-button>
						</view>
					</view>
				</u-list-item>
			</u-list>
		</view>
		<u-modal :show="showConfirm" @confirm="handleDelAddr" @cancel="showConfirm = false" :showCancelButton="true"
				title="删除地址" content='确定删除该地址？'></u-modal>
		
	</view>
</template>

<script>
	import {
		fetchAddrList,
		delAddr,
		defaultAddr
	} from "@/apis/user.js";
	import {
		obj2Url,
	} from "@/util";
	export default {
		data() {
			return {
				addrList: [],
				showConfirm: false,
				currItem: null
			};
		},
		onShow() {
			this.fetchData()
		},
		methods: {
			async fetchData() {
				const res = await fetchAddrList()
				if (res.MsgCode != 0) return
				console.log('fetchData', res)
				this.addrList = res.data.AddrList
			},
			handleEditaddr(item){
				console.log('handleEditaddr', item)
				uni.navigateTo({
					url:'/pages/my/address/addAddress?' + obj2Url(item)
				})
			},
			handleModalConfirm(item){
				this.currItem = item
				this.showConfirm = true
			},
			async handleDelAddr() {
				const currItem = this.currItem
				if (!currItem) return;
				console.log('handleDelAddr', currItem)
				const res = await delAddr({
					AddrId: currItem.AddrId,
				})
				console.log('handleDelAddr', res)
				
				if (res.MsgCode != 0) return
				this.showConfirm = false
				uni.showToast({
					title: '删除成功！',
					duration: 500,
					icon: 'success'
				});
				setTimeout(this.fetchData, 500)
			},
			async handleDefaultAddr(item) {
				const res = await defaultAddr({
					AddrId: item.AddrId,
				})
				this.fetchData()
				console.log('handleDefaultAddr', res)
			},
			scrolltolower() {},
			onNavigationBarButtonTap(e) {
				uni.navigateTo({
					url: '/pages/my/address/addAddress'
				})
			},
		},
	}
</script>

<style lang="scss">
	.addr {
		.top-bg {
			width: 100%;
			height: 120rpx;
			background-color: #007aff;
			z-index: -1;
		}

		.addr-wrap {
			margin-top: -72rpx;

			.fb-item {
				background-color: #fff;
				box-shadow: $s-bg-shadow;
				border-radius: $s-border-radius;
				font-size: 24rpx;

				.header {
					color: #666;
				}

				.opt {
					background-color: #f6faff;
					border-bottom-left-radius: $s-border-radius;
					border-bottom-right-radius: $s-border-radius;
					.default{
						color: #777;
						font-size: 24rpx;
					}
					::v-deep .u-button--mini {
						margin-left: 20rpx !important;
						width: 100rpx;
						background-color: #fff;
						box-shadow: $s-bg-shadow;
						// border-radius: $s-border-radius;
						font-size: 24rpx;
						margin: 0;
						color: $s-text-color-brand
					}
					::v-deep .u-button--primary{
						border-width: 0;
						width: 125rpx;
					}
				}

			}
		}
	}
</style>
